var api = require('../../util/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasUserInfo: true,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    clickNum: 1,
    lastTapTime: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    if (wx.getStorageSync("userInfo")) {
      this.isBound(function (isBound) {
        if (isBound) {
          api.syncCurrUser().then(res => {
            wx.showLoading({
              title: "辽河监督欢迎您",
              mask: true
            });
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/gsjj/gsjj'
              })
            }, 1600);
          });
        } else {
          wx.redirectTo({
            url: '/pages/index/home/home'
          })
        }
      });
    } else {
      this.setData({
        hasUserInfo: false
      });
    }

    this.submitPage();
  },

  wxLogin: function (i) {
    var _this = this;
    wx.login({
      success: res => {
        wx.request({
          url: api.url.jscode2session,
          data: {
            appid: api.appid,
            avatarUrl: i.detail.userInfo.avatarUrl,
            code: res.code,
            nickName: i.detail.userInfo.nickName,
            gender: i.detail.userInfo.gender
          },
          method: 'post',
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          success: function (res) {
            wx.setStorageSync('userInfo', i.detail.userInfo)
            wx.setStorageSync('appid', api.appid)
            wx.setStorageSync('openid', res.data.openid)
            _this.onLoad();
          },
          fail: function (res) {

          }
        })
      },
      fail: n => {
        wx.hideLoading()
        showErrorToast("微信登录失败");
      }
    });
  },
  isBound: function (sfn) {
    wx.request({
      url: api.url.isBound,
      data: {
        appid: api.appid,
        openid: wx.getStorageSync('openid')
      },
      method: 'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res2) {
        return sfn(res2.data.data);
      },
      fail: function (res) {
        return false;
      }
    })
  },
  continuousClick(e) {
    var me = this;
    var curTime = e.timeStamp //获取时间戳
    var lastTime = me.data.lastTapTime; //第一次获取为零
    console.log("上一次点击时间：" + lastTime)
    console.log("这一次点击时间：" + curTime)

    if (curTime - lastTime < 500) {
      me.setData({
        clickNum: me.data.clickNum + 1
      })
    } else {
      me.setData({
        clickNum: 1
      })
    }
    console.log("me.data.clickNum:", me.data.clickNum);
    if (me.data.clickNum == 3) { //想定义双击就换成2，三击就换成3...
      console.log("点击3次，进入"); //这里写想要执行的代码块
      wx.redirectTo({
        url: '/pages/index/login/login'
      })
    }
    me.setData({
      lastTapTime: curTime
    })
  },
  //页面内容接入 页面路径推送
  submitPage() {
    wx.request({
      url: api.url.wxAccessToken,
      data: {
        appid: api.appid
      },
      method: 'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        let access_token = res.data.access_token;
        wx.request({
          url: `https://api.weixin.qq.com/wxa/search/wxaapi_submitpages?access_token=${access_token}`,
          data: {
            "pages": [{
              "path": "pages/wx/pagesubmit",
              "query": "name=辽河监督"
            }]
          },
          method: 'post',
          header: {
            'content-type': 'application/json'
          },
          success: function (res2) {
            console.log('微信页面推送：', res2.data)
          },
          fail: function (res) {
            return false;
          }
        })
      },
      fail: function (res) {
        return false;
      }
    })
  }
})